package com.xpsoft.oa.service.statistics.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.oa.dao.statistics.StaffRecruitInfoDao;
import com.xpsoft.oa.dao.statistics.StaffRecruitapplyDao;
import com.xpsoft.oa.model.statistics.StaffRecruitInfo;
import com.xpsoft.oa.model.statistics.StaffRecruitapply;
import com.xpsoft.oa.service.statistics.StaffRecruitapplyService;

public class StaffRecruitapplyServiceImpl extends BaseServiceImpl<StaffRecruitapply> implements StaffRecruitapplyService{
	private StaffRecruitapplyDao dao;
	@Autowired
	private StaffRecruitInfoDao staffRecruitInfoDao;
	public StaffRecruitapplyServiceImpl(StaffRecruitapplyDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveStaffRecruitapply(StaffRecruitapply staffRecruitapply,
			String sriDataList) {
		// TODO Auto-generated method stub
		Boolean isNew = true;//新增标志
		if(staffRecruitapply.getId()!=null){
			isNew = false;
		}
		dao.save(staffRecruitapply);
		if(StringUtils.isNotBlank(sriDataList)){
			List<StaffRecruitInfo> staffRecruitInfoList = JsonUtil.fromJsonTypesWithDate(sriDataList,
					new TypeToken<List<StaffRecruitInfo>>() {
					}.getType());
			if(!isNew){//修改
				List<Long> idList = new ArrayList<Long>();
				for(StaffRecruitInfo staffRecruitInfo:staffRecruitInfoList){
					if(staffRecruitInfo.getId()!=null){//修改的记录
						idList.add(staffRecruitInfo.getId());
					}
				}
				if(idList.size()>0){//先进行删除
					staffRecruitInfoDao.deleteStaffRecruitInfos(staffRecruitapply.getId(), idList);
				}
			}
			for(StaffRecruitInfo staffRecruitInfo:staffRecruitInfoList){
				staffRecruitInfo.setStaffRecruitapply(staffRecruitapply);
				if(staffRecruitInfo.getId()!=null){
					StaffRecruitInfo oldStaffRecruitInfo = staffRecruitInfoDao.get(staffRecruitInfo.getId());
					staffRecruitInfo.setStatus(oldStaffRecruitInfo.getStatus());
					staffRecruitInfoDao.merge(staffRecruitInfo);
				}else{
					staffRecruitInfo.setStatus(0);
					staffRecruitInfoDao.save(staffRecruitInfo);
				}
			}
		}
	}

}